package com.example.tellwin.home.presenter;

import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.api.TwjfApi;
import com.example.tellwin.base.RxPresenter;
import com.example.tellwin.base.SimpleCallback;
import com.example.tellwin.home.bean.ContentBean;
import com.example.tellwin.home.bean.HistoryBean;
import com.example.tellwin.home.bean.HotSearchBean;
import com.example.tellwin.home.bean.QuestionContentBean;
import com.example.tellwin.home.contract.SearchContract;
import com.example.tellwin.utils.HttpUtils;
import com.example.tellwin.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import okhttp3.RequestBody;

/**
 * @author : LinZePeng
 * @date : 2020/11/26
 * @Describe ：
 **/
public class SearchPresenter extends RxPresenter<SearchContract.View> implements SearchContract.Presenter<SearchContract.View> {


    private TwjfApi mApi;
    private int pages;
    private int pageNum;
    private final int pageSize = 10;
    private String searchName;
    private boolean isNotMore = true;

    @Inject
    public SearchPresenter(TwjfApi mApi) {
        this.mApi = mApi;
    }

    @Override
    public void historySearch() {
        mApi.historySearch(new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                List<HistoryBean> historyBeans = contentBean.getHistory();
                if (historyBeans == null) {
                    historyBeans = new ArrayList<>();
                }
                mView.historySearchResult(historyBeans);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void hotSearch() {
        mApi.hotSearch(new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                List<HotSearchBean> hotSearchBeans = contentBean.getHotSearch();
                if (hotSearchBeans == null) {
                    hotSearchBeans = new ArrayList<>();
                }
                mView.hotSearchResult(hotSearchBeans);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void search(String seacrhName) {
        this.searchName = seacrhName;
        pageNum = 1;
        pages = 0;
        isNotMore = false;
        mApi.search(seacrhName, pageNum + "", pageSize + "", new SimpleCallback<QuestionContentBean>(mView) {
            @Override
            public void onSuccess(QuestionContentBean contentBean) {
                pageNum = contentBean.getPageNum();
                pages = contentBean.getPages();
                if (contentBean.getSearchList() == null || contentBean.getSearchList().size() == 0) {
                   // ToastUtil.show(MainApplication.getCurActivity(), R.string.none_data);
                    isNotMore = true;
                    mView.searchFail();
                    return;
                }
                mView.searchResult(contentBean.getSearchList());
            }

            @Override
            public void onFail(String msg) {
                mView.searchFail();
            }
        });
    }

    @Override
    public void loadMore() {
        if (isNotMore) {
            return;
        }
        if (pages != 0 && pageNum == pages) {
            ToastUtil.show(MainApplication.getCurActivity(), R.string.none_data);
            isNotMore = true;
            return;
        }
        pageNum++;
        mApi.search(searchName, pageNum + "", pageSize + "", new SimpleCallback<QuestionContentBean>(mView) {
            @Override
            public void onSuccess(QuestionContentBean contentBean) {
                pageNum = contentBean.getPageNum();
                pages = contentBean.getPages();
                if (contentBean.getSearchList() == null || contentBean.getSearchList().size() == 0) {
                    ToastUtil.show(MainApplication.getCurActivity(), R.string.none_data);
                    isNotMore = true;
                    return;
                }
                mView.loadMoreResult(contentBean.getSearchList());
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void likeQuestion(String questionId, int position) {
        RequestBody requestBody = HttpUtils.getRequestBody("followType", "0" + "", "relationId", questionId, "relationType", "0");
        mApi.follow(requestBody, new SimpleCallback<Object>(mView) {
            @Override
            public void onSuccess(Object o) {
                mView.likeQuestionResult(position);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }
}
